package com.example.aicooking.service.imp;

import com.example.aicooking.entity.Works;
import com.example.aicooking.entity.dto.IngredientDTO;
import com.example.aicooking.entity.vo.WorksListVO;
import com.example.aicooking.mapper.IngredientMapper;
import com.example.aicooking.mapper.WorksMapper;
import com.example.aicooking.service.WorksService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class WorksServiceImpl implements WorksService {
    @Autowired
    private WorksMapper worksMapper;
    @Autowired
    private IngredientMapper ingredientMapper;
    /**
     * 作品列表
     * @return
     */
    @Override
    public List<WorksListVO> getWorksList() {
        List<WorksListVO> works = worksMapper.selectWorksWithInfo();
        return works.stream().map(work -> {
            WorksListVO vo = new WorksListVO();
            BeanUtils.copyProperties(work, vo);

            // 获取食材列表
            List<IngredientDTO> ingredients = ingredientMapper.findIngredientsByWorkId(work.getWorksId().intValue());
            // 处理食材显示
            if (ingredients != null && !ingredients.isEmpty()) {
                StringBuilder sb = new StringBuilder();
                int count = 0;
                for (IngredientDTO ingredient : ingredients) {
                    if (count < 2) {
                        if (count > 0) {
                            sb.append("、");
                        }
                        sb.append(ingredient.getIngredientName());
                        count++;
                    } else {
                        sb.append("......");
                        break;
                    }
                }
                vo.setIngredients(sb.toString());
            } else {
                vo.setIngredients("暂无食材信息");
            }

            return vo;
        }).collect(Collectors.toList());
    }

    /**
     * 查看作品详情
     * @param worksId
     * @return
     */
    @Override
    public Works getWorksDetail(Integer worksId) {
        Works works = worksMapper.selectById(worksId);
        if (works == null) {
            throw new RuntimeException("作品不存在");
        }
        return works;
    }

    /**
     * 添加作品
     */
    @Override
    public Works addWorks(Works works) {
        works.setStatus(0); // 默认未发布
        works.setCommentCount(0);
        works.setLikeCount(0);
        int result = worksMapper.insertWorks(works);
        if (result <= 0) {
            throw new RuntimeException("添加作品失败");
        }
        return works;
    }

    /**
     * 发布/取消发布作品
     */
    @Override
    public void toggleWorksStatus(Integer worksId, Integer userId, Integer status) {
        int result = worksMapper.updateWorksStatus(worksId, userId, status);
        if (result <= 0) {
            throw new RuntimeException("更新作品状态失败");
        }
    }

    /**
     * 删除作品
     */
    @Override
    public void deleteWorks(Integer worksId, Integer userId) {
        int result = worksMapper.deleteWorks(worksId, userId);
        if (result <= 0) {
            throw new RuntimeException("删除作品失败");
        }
    }

    /**
     * 获取用户的所有作品
     */
    @Override
    public List<Works> getUserWorks(Integer userId) {
        return worksMapper.selectUserWorks(userId);
    }



}

